<?php
namespace App\Http\Controllers;


use Illuminate\Http\Request;
use DB;
use App\Http\Common;
//use Illuminate\Support\Facades\Session;
//use Illuminate\Session;
use Mail;

class LoginController extends Controller {

    use Common;
    
    public function getIndex()
    {
        $admin = session()->get('adminInfo');

        if(!empty($admin)){
            return redirect('/event');
        }

        return response()->view('login')->header('Cache-Control', 'no-cache,no-store,must-revalidate');
    }

    public function postLogin(Request $request)
    {
        $user = $request->input('user', null);

        $password = $request->input('password', null);
        $password = hash('sha256', $password);

        if(empty($user) || empty($password)){
            return $this->errorReturn('此信息必填，请重新输入'); 
        }
        
        $userInfo = DB::connection('scool_business')->table('user')
                                           ->where('name', $user)->where('password', $password)
                                           ->first();

        if(empty($userInfo)){
            return $this->errorReturn('用户名或密码错误，请重新输入'); 
        }

        if($userInfo){

            $session = $request->session();

            $session->put('adminInfo', $userInfo);

            return $this->successReturn('success'); 

        }else{
            return $this->errorReturn('用户名或密码错误，请重新输入'); 
        }


    }

    public function getLoginOut()
    {
        session()->forget('adminInfo');

        return redirect('/');
    }

    public function getRepassword()
    {
        return view('repassword' , ['current' => '', 'email' => session('adminInfo')->email]);
    }

    public function postRepassword(Request $request)
    {
        $adminInfo = session('adminInfo');

        $input = $request->all();

        $md5CurrentPassword = md5($input['currentPassword'].$adminInfo->salt);

        if($md5CurrentPassword != $adminInfo->password){
            return $this->errorReturn('当前密码输入不正确，请重新输入');
        }

        if($input['password'] != $input['repassword']){
            return $this->errorReturn('两次输入的密码不匹配，请重新输入');
        }

        if(!preg_match('/^(?=.*[a-z])[a-z0-9]{6,16}$/i',$input['password'])){
            return $this->errorReturn('请输入6-16位字符，由字母、数字和组成');
        }

        DB::table('users')->where('id', $adminInfo->id)->update(['password' => md5($input['password'].$adminInfo->salt), 'update_time' => time() ]);

        $userInfo = DB::table('users')->where('id', $adminInfo->id)->first();

        session()->put('adminInfo', $userInfo);

        return $this->successReturn('更新成功');

    }


    public function Insert()
    {
        DB::connection('admin')->table('user')->insert(['user'=>'zhuren','password'=>md5('zhuren123111111'),'create_time'=>time(),'salt'=>111111, 'status' => 1]);
    }

    public function testMail()
    {   
        $data = [];
        Mail::send('emails.welcome', $data, function($message) use ($data)
        {   
            $message->from('scool@scool.cc', 'Scool');

            $message->to('tieyuz@uci.edu')->subject('账号确认邮件——来自Scool!');
        });

    

    }

}
